import log from 'electron-log/main'
import { app, ipcMain } from 'electron'
import os from 'os'

// 初始化日志
log.initialize()

// 获取系统信息
const getSystemInfo = () => ({
  timestamp: new Date().toISOString(),
  version: app.getVersion(),
  os: `${os.platform()} ${os.release()}`
})

// 封装 Log 对象
const Log = {
  info: (message: string) => {
    const systemInfo = getSystemInfo()
    log.info(
      `[App Version: ${systemInfo.version}] [${systemInfo.os}] - ${message}`
    )
  },
  error: (message: string) => {
    const systemInfo = getSystemInfo()
    log.error(
      `[App Version: ${systemInfo.version}] [${systemInfo.os}] - ${message}`
    )
  },
  warn: (message: string) => {
    const systemInfo = getSystemInfo()
    log.warn(
      `[App Version: ${systemInfo.version}] [${systemInfo.os}] - ${message}`
    )
  },
  verbose: (message: string) => {
    const systemInfo = getSystemInfo()
    log.verbose(
      `[App Version: ${systemInfo.version}] [${systemInfo.os}] - ${message}`
    )
  },
  debug: (message: string) => {
    const systemInfo = getSystemInfo()
    log.debug(
      `[App Version: ${systemInfo.version}] [${systemInfo.os}] - ${message}`
    )
  },
  silly: (message: string) => {
    const systemInfo = getSystemInfo()
    log.silly(
      `[App Version: ${systemInfo.version}] [${systemInfo.os}] - ${message}`
    )
  }
}

interface LogMessage {
  type: 'info' | 'error' | 'warn' | 'verbose' | 'debug' | 'silly'
  message: string
}
ipcMain.on('log', (_, input: LogMessage) => {
  Log[input.type](input.message)
})

export default Log
